/* *********************************************************************
 R2 module of MPX for Group 3 -2.1
 File Name:   PCB.c
 Author:    Y.Tu  
 Version 2.1 
 Date:  10/01/2008
 Purpose: Provide functions for Internal operations of PCB  for R2 module 

************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "PCB.h"


/* allocate memory to PCB, return NULL if unsuccessful */ 
PCB* allo_pcb (void ){

PCB*  p;
p = (PCB*)sys_alloc_mem(sizeof(PCB)); 
return p; 

}


/*Initialize a PCB by providing class, name, and priority, state was set to READY as default */ 
PCB* set_pcb(PCB* p,char name[MAX_PCB_NAME],int class,char *state, int priority) {

if(p==NULL)
printf("Have not been assigned memory successfully.");

else
{
p->pcb_class =class;
strcpy(p->pcb_name, name);
strcpy(p->pcb_state,state);
p->pcb_priority=priority;

/* 
p->pcb_memory=NULL;  
p->pcb_resource=NULL;
p->pcb_context=NULL;
p->pcb_account=NULL;
*/
}

return p;
}


void free_pcb(PCB* p){
sys_free_mem( (void*)p );

}


/********************************************
  This file is used to test the correctness of
  PCB.h and PCB.c

*********************************************/
/*****
void main () {

PCB *p1,*p2;
p1 = allo_pcb( );

p1 = set_pcb(p1,"Tiger",1,100);

printf("The1st PCB\n");
printf("The PCB has the following elements:\n");
printf("Name is %s\n", p1->pcb_name);
printf("class is %d\n", p1->pcb_class);
printf("Status is %d\n",p1->pcb_state);
printf("Priority is %d\n",p1->pcb_priority);
free_pcb (p1); 

p2 = allo_pcb();
p2= set_pcb(p2,"Bird",2,200);

printf("The 2nd PCB\n");
printf("The PCB has the following elements:\n");
printf("Name is %s\n", p2->pcb_name);
printf("class is %d\n", p1->pcb_class);
printf("Status is %d\n",p2->pcb_state);
printf("Priority is %d\n",p2->pcb_priority);

free_pcb (p2); 

return;
}

******************************************/ 

